from selenium import webdriver
import time
import ddddocr

ocr = ddddocr.DdddOcr()

options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(options=options)
driver.get("https://passport.haodf.com/user/showlogin")
driver.maximize_window()
time.sleep(3)
driver.find_element("xpath", "//ul/li[@class='userlrv1_lg userlrv1_lg2']").click()
time.sleep(1)
driver.find_element("xpath", "//input[@class='userlrv1_i_text userlrv1_i_text_1 userName']").send_keys("12313")
driver.find_element("xpath", "//input[@class='userlrv1_i_text userlrv1_i_text_1 password']").send_keys("22571040wlq")
# 更改同意的小点
driver.execute_script("""
var parentElement = document.querySelector('.m-privacy.m-privacy-normal');
if (parentElement) {
    var targetElement = parentElement.querySelector('.status_icon.unselected');
    if (targetElement) {
        targetElement.className = 'status_icon selected';
    }
}
""")
# 然后勾选框
driver.find_element("xpath", "//div[@class='status_icon selected']").click()
# 点击登录按钮
driver.find_element("xpath", "//a[@class='userlrv1_blue_btn userlrv1_blue_btn2 f-yahei loginbynormal']").click()
time.sleep(3)
# 密码错误，获取验证码
driver.find_element("xpath", "//img[@id='registercaptcha']").screenshot("好大夫验证码.png")
image = open("好大夫验证码.png", "rb").read()
result = ocr.classification(image)
print(result)
driver.find_element("xpath", "//input[@class='userlrv1_i_text2 userlrv1_i_text2_1 checkCode ']").send_keys(result)
# 最后在点击登录按钮
driver.find_element("xpath", "//a[@class='userlrv1_blue_btn userlrv1_blue_btn2 f-yahei loginbynormal']").click()
driver.quit()
